<template>
    <div class="pageContainer">
        <el-container class="bg-white rounded-lg">
            <el-header style="border-bottom: 1px solid #eeeeee; height: 110px;">
                <div>
                    <el-form :model="searchform" label-width="80px" class="mb-3 flex justify-between items-center pt-5">
                        <el-form-item label="关键词" class="mb-0">
                            <el-input v-model="searchform.keyword" clearable></el-input>
                        </el-form-item>
                        <el-form-item class="mb-0">
                            <el-button type="primary" @click="onSearch">搜索</el-button>
                            <el-button @click="onClearSearch">重置</el-button>
                        </el-form-item>
                    </el-form>
                </div>
                <div class="flex items-center justify-between">
                    <el-button type="primary" @click="onAdd">新增</el-button>
                    <el-button type="primary" text @click="onSearch">
                        <el-icon :size="20">
                            <Refresh />
                        </el-icon>
                    </el-button>
                </div>
            </el-header>
            <el-container>
                <div class="w-full">
                    <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
                        <el-table-column label="序号" width="60" align="center" fixed="left" prop="rank">
                            <template #default="{ $index, row }">
                                {{ (currentPage - 1) * limit + $index + 1 }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="name" label="优惠券名称" min-width="150" />
                        <el-table-column prop="type" label="类型" min-width="100">
                            <template #default="{ row }">
                                {{ row.type === 0 ? '满减券' : '折扣券' }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="value" label="面值" min-width="100">
                            <template #default="{ row }">
                                {{ row.type === 0 ? `￥${row.value}` : `${row.value}折` }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="amount" label="发行量" min-width="100" />
                        <el-table-column prop="min_price" label="最低消费" min-width="120">
                            <template #default="{ row }">
                                ￥{{ row.min_price }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="start_time" label="有效期" min-width="300">
                            <template #default="{ row }">
                                {{ formatDateTime(row.start_time) }} 至 {{ formatDateTime(row.end_time) }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="status" label="状态" min-width="100">
                            <template #default="{ $index, row }">
                                <el-switch v-model="row.status" :active-value="1" :inactive-value="0" size="small" :lazy="true"
                                    @change="onStatusChange(row)" />
                            </template>
                        </el-table-column>
                        <el-table-column fixed="right" label="操作" min-width="120" align="center">
                            <template #default="scope">
                                <el-button link type="primary" @click="onEdit(scope.row)">修改</el-button>
                                <el-popconfirm title="确定删除该优惠券吗？" @confirm="onDelete(scope.row)">
                                    <template #reference>
                                        <el-button type="primary" link>删除</el-button>
                                    </template>
                                </el-popconfirm>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div style="height: 50px;" class="flex items-center justify-center">
                        <el-pagination layout="prev, pager, next" background :current-page="currentPage"
                            :total="totalCount" :page-size="10" @current-change="onPageChange" />
                    </div>
                </div>
            </el-container>
        </el-container>
        <FormDrawer ref="drawerRef" :title="drawerTitle" @submit="onSubmit">
            <el-form ref="formRef" :model="form" :rules="rules" label-width="100">
                <el-form-item label="优惠券名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入优惠券名称" clearable />
                </el-form-item>
                <el-form-item label="类型" prop="type">
                    <el-select v-model="form.type" placeholder="请选择优惠券类型" style="width: 240px">
                        <el-option label="满减券" :value="0" />
                        <el-option label="折扣券" :value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item :label="form.type === 0 ? '优惠金额' : '折扣比例'" prop="value">
                    <el-input v-model="form.value" :placeholder="form.type === 0 ? '请输入优惠金额' : '请输入折扣比例（0-100）'" clearable />
                </el-form-item>
                <el-form-item label="发行量" prop="amount">
                    <el-input v-model="form.amount" placeholder="请输入发行量" clearable />
                </el-form-item>
                <el-form-item label="最低消费" prop="min_price">
                    <el-input v-model="form.min_price" placeholder="请输入最低消费金额" clearable />
                </el-form-item>
                <el-form-item label="有效期" prop="dateRange">
                    <el-date-picker
                        v-model="form.dateRange"
                        type="datetimerange"
                        range-separator="至"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                    />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-switch v-model="form.status" :active-value="1" :inactive-value="0" />
                </el-form-item>
                <el-form-item label="排序" prop="order">
                    <el-input-number v-model="form.order" :min="1" :max="1000" />
                </el-form-item>
            </el-form>
        </FormDrawer>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { getCouponList, addCoupon, updateCouponById, deleteCouponById, updateCouponStatusById } from '../../api/coupon'
import { formatDateTime, toISO8601 } from '../../composables/date'
import FormDrawer from '../../components/FormDrawer.vue'
import { useTableSearch, useForm } from '../../composables/common'

// 定义查询表单
const searchform = reactive({
    keyword: "",
})

const { 
    tableRowClassName,
    onSearch,
    getData,
    onClearSearch,
    onPageChange,
    currentPage,
    tableData,
    totalCount,
    limit
} = useTableSearch({
    searchform,
    getTableList: getCouponList
})

// 表单
const form = reactive({
    id: 0,
    name: '',
    type: 0,
    value: '',
    amount: '',
    min_price: '',
    dateRange: [],
    status: 1,
    order: 50
})

// 表单规则
const rules = {
    name: [{ required: true, message: '优惠券名称不能为空', trigger: 'blur' }],
    type: [{ required: true, message: '请选择优惠券类型', trigger: 'change' }],
    value: [{ required: true, message: '请输入优惠金额/折扣比例', trigger: 'blur' }],
    amount: [{ required: true, message: '请输入发行量', trigger: 'blur' }],
    min_price: [{ required: true, message: '请输入最低消费金额', trigger: 'blur' }],
    dateRange: [{ required: true, message: '请选择有效期', trigger: 'change' }],
    status: [{ required: true, message: '请选择状态', trigger: 'change' }],
}

const { 
    formRef,
    drawerRef,
    drawerTitle,
    onAdd,
    onEdit,
    onDelete,
    onSubmit,
    onStatusChange
} = useForm({
    title: '优惠券',
    form,
    tableData,
    getData,
    add: addCoupon,
    update: updateCouponById,
    delete: deleteCouponById,
    updateStatus: updateCouponStatusById,
    formDataInit: (form) => {
        // 修改优惠劵
        if(form.id != null && form.id > 0) {
            // 处理range时间
            // console.log('formDataInit-form', form)
            form.dateRange[0] = formatDateTime(form.start_time)
            form.dateRange[1] = formatDateTime(form.end_time)
        } else {
            // 新增优惠劵
            form.order = 50
        }
    }, 
    beforeSubmit: (form) => {
        // 处理时间格式
        // console.log('beforeSubmit-form', form)
        if (form.dateRange && form.dateRange.length === 2) {
            form.start_time = toISO8601(form.dateRange[0])
            form.end_time = toISO8601(form.dateRange[1])
        }
        return true
    }
})
</script>
